package businessLayer.businessObjects;

import java.util.ArrayList;

import dataAccessLayer.abstractFactory.Factory;
import dataAccessLayer.abstractFactory.daos.DAOcities;
import dataAccessLayer.abstractFactory.daos.DAOcustomers;
import dataAccessLayer.visualObject.VOcity;
import dataAccessLayer.visualObject.VOcustomer;

public class CustomersBusinessObject 
{
	private DAOcustomers daoCustomers;
	private DAOcities daoCities;

	public CustomersBusinessObject(Factory factory)
	{
		daoCustomers = factory.getDAOcustomers();
		daoCities = factory.getDAOcities();
	}

	public Object[][] getCustomers()
	{
		ArrayList<VOcustomer> customers;

		customers = daoCustomers.listCustomers();

		String[] cities = new String[customers.size()];

		VOcity vOcity = new VOcity();

		for (int i = 0; i < cities.length; i++) 
		{
			vOcity.setCode(customers.get(i).getCity());

			cities[i] = (daoCities.searchCityByCode(vOcity)).getName();
		}

		Object[][] resul = new Object[customers.size()][4];
		
		for (int i = 0; i < cities.length; i++) 
		{
			for (int j = 0; j < 8; j++) 
			{
				switch(j)
				{
				case 0:
					resul[i][j] = customers.get(i).getName();
					break;
				case 1:
					resul[i][j] = customers.get(i).getAddress();
					break;
				case 2:
					resul[i][j] = cities[i];
					break;
				case 3:
					resul[i][j] = customers.get(i).getTelephone();
					break;
				}
			}
		}

		return resul;
	}

	public boolean insertCustomer(String name, String address, int city,String telephone) 
	{
		VOcustomer vOcustomer = new VOcustomer();
		vOcustomer.setName(name);
		vOcustomer.setAddress(address);
		vOcustomer.setCity(city);
		vOcustomer.setTelephone(telephone);
		
		int code = daoCustomers.insertCustomer(vOcustomer);
		
		if(code>0)
		{
			return true;
		}
		
		return false;
	}
}
